package cn.com.bluemoon.daps.system.service.impl;

import cn.com.bluemoon.daps.common.constant.DapConstant;
import cn.com.bluemoon.daps.system.entity.DapSystemProperty;
import cn.com.bluemoon.daps.system.mapper.DapSystemPropertyMapper;
import cn.com.bluemoon.daps.system.service.DapSystemPropertyService;
import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 系统字典表 服务实现类
 * </p>
 *
 * @author admin
 * @since 2021-12-29
 */
@Slf4j
@Service
public class DapSystemPropertyServiceImpl extends ServiceImpl<DapSystemPropertyMapper, DapSystemProperty>
        implements DapSystemPropertyService {

    @Autowired
    private DapSystemPropertyMapper dapSystemPropertyMapper;

    @Override
    public IPage<DapSystemProperty> systemPropertyPage(Integer pageSize, Integer pageNum, String key) {

        LambdaQueryWrapper<DapSystemProperty> wrapper = new LambdaQueryWrapper<>();
        wrapper.and(!StringUtils.isEmpty(key), w -> w.like(DapSystemProperty::getPropertyGroup, key)
                .or().like(DapSystemProperty::getPropertyKey, key)
                .or().like(DapSystemProperty::getPropertyValue, key)
                ).orderByAsc(DapSystemProperty::getPropertyGroup)
                 .orderByDesc(DapSystemProperty::getCreateTime);
        Page<DapSystemProperty> page = new Page<>(pageNum, pageSize);
        return this.getBaseMapper().selectPage(page, wrapper);
    }

    @Override
    public Boolean insertOrUpdateProperty(DapSystemProperty property) {

        if (property.getId() == null) {
            property.setCreateTime(LocalDateTime.now());
            // 新增给定参数值唯一，查询参数标识
            if(property.getPropertyGroup().equals(DapConstant.RULE_TYPE)){
                List<DapSystemProperty> dapSystemProperties = this.getBaseMapper().selectList(new LambdaQueryWrapper<DapSystemProperty>()
                        .eq(DapSystemProperty::getPropertyGroup, property.getPropertyGroup()));
                // 查询参数值中的最大值
                Optional<DapSystemProperty> max = dapSystemProperties.stream().max(Comparator.comparing(DapSystemProperty::getPropertyValue));
                Integer value = max.map(dapSystemProperty -> Integer.parseInt(dapSystemProperty.getPropertyValue()) + 1).orElse(1);
                property.setPropertyValue(value.toString());
            }
        } else {
            property.setLastUpdateTime(LocalDateTime.now());
        }
        return checkProperty(property) && saveOrUpdate(property);
    }

    @Override
    public DapSystemProperty propertyInfo(String id) {
        return this.getBaseMapper().selectById(id);
    }

    @Override
    public Boolean deleteProperty(String id) {
        int total = this.getBaseMapper().deleteById(id);
        return total == 1;
    }

    @Override
    public Map<String, List<DapSystemProperty>> getByPropertyGroups(List<String> propertyGroups) {
        List<DapSystemProperty> dapSystemProperties = dapSystemPropertyMapper.getByPropertyGroups(propertyGroups);
        if (dapSystemProperties == null) {
            return null;
        }
        return dapSystemProperties.stream().collect(Collectors.groupingBy(DapSystemProperty::getPropertyGroup));
    }

    private Boolean checkProperty(DapSystemProperty dapSystemProperty){
        // 查询当前参数
        List<DapSystemProperty> dapSystemProperties = this.getBaseMapper().selectList(new LambdaQueryWrapper<DapSystemProperty>()
                        .eq(DapSystemProperty::getPropertyGroup, dapSystemProperty.getPropertyGroup())
                        .ne(dapSystemProperty.getId() != null,
                                DapSystemProperty::getId, dapSystemProperty.getId())
                        .eq(DapSystemProperty::getPropertyKey, dapSystemProperty.getPropertyKey()));
        if(dapSystemProperties.isEmpty()){
            return true;
        }
        DapSystemProperty property = dapSystemProperties.stream().findFirst().get();
        return dapSystemProperty.getId() != null && property.getId().equals(dapSystemProperty.getId());
    }
}
